#!/bin/bash

base_num=$1
add_num=$2
add_times=5
experiments_times=$3


#基础的文件夹名称
#one-step
checkpoint_one_step="checkpoint_one_step_all.tar.gz"
#two-step
checkpoint_two_step_first="checkpoint_two_step_first.tar.gz"
checkpoint_two_step_second="checkpoint_two_step_second.tar.gz"


echo "base_num: $base_num , add_num: $add_num , experiments_times: $experiments_times"


cur_experiments_times=1
need_add_times=0

while (($cur_experiments_times <= $experiments_times))
do
    echo "-----------------------cur_experiments_times: $cur_experiments_times----------------"

    while (($need_add_times < $add_times))
    do
        base_num=`expr $base_num + $add_num`
        echo "the base_num is ${base_num}"

        #创建文件夹
        if [ $cur_experiments_times == 1 ]
        then
            mkdir /tmp/redis_${base_num}
        fi
        cd /tmp/redis_${base_num}

        filename_export_one_step="${cur_experiments_times}_${checkpoint_one_step}"
        filename_export_two_step_first="${cur_experiments_times}_${checkpoint_two_step_first}"
        filename_export_two_step_second="${cur_experiments_times}_${checkpoint_two_step_second}"


        echo "---------------------cur_experiments_times is $cur_experiments_times---------------------" >> time_of_checkpoint
        echo "" >> time_of_checkpoint

        #创建容器
        podman run -d -p 6379:6379 --name redis my-redis
        echo "the container is run successfully"
        #增加数据 
        cd /home/yang/experiments-based-on-pre-checkpoint/pkg/
        ./sr -n $base_num
        sleep 1
        echo "add ${base_num} keys to the redis container successfuly"

        cd /tmp/redis_${base_num}
                
        #one step
        echo "---------------------start one-step checkpoint---------------------"
        echo "---------------------start one-step checkpoint---------------------" >> time_of_checkpoint

        { time podman container checkpoint -e ./${filename_export_one_step} -l; } 2>> time_of_checkpoint
        echo "" >> time_of_checkpoint
        echo "one-step checkpoint successfuly"
        echo ""

        podman container restore -l
        sleep 2
        echo ""

        #two step
        echo "---------------------start two-step checkpoint---------------------"
        echo "---------------------start two-step checkpoint---------------------" >> time_of_checkpoint
        echo "" >> time_of_checkpoint

        echo "time of checkpoint_two_step_first" >> time_of_checkpoint
        { time podman container checkpoint -P -e ./${filename_export_two_step_first} -l; } 2>> time_of_checkpoint
        echo "" >> time_of_checkpoint
        echo "pre-checkpoint successfuly"
        echo ""
        sleep 2

        
        echo "time of checkpoint_two_step_second" >> time_of_checkpoint
        { time podman container checkpoint --with-previous -e ./${filename_export_two_step_second} -l; } 2>> time_of_checkpoint
        echo ""
        echo "checkpoint successfuly"
        echo ""
        echo "" >> time_of_checkpoint

        podman rm redis
        echo "remove container successfuly"
        echo ""
        sleep 2

        #restore
        echo "---------------------start restore from two tarball---------------------"
        echo "---------------------start restore from two tarball----------------" >> time_of_checkpoint

        { time podman container restore -i ./${filename_export_two_step_second} --import-previous ./${filename_export_two_step_first}; } 2>>time_of_checkpoint
        echo "restore from two tarball successfuly"
        echo ""
        echo "" >> time_of_checkpoint

        #delete container
        podman stop redis
        podman rm redis
        echo "remove container successfuly"
        echo ""
        
        #打印相关信息到指定文件
        echo "---------------------cur_experiments_times is $cur_experiments_times---------------" >> size
        ls -l $cur_experiments_times* | awk '{print $9 "\t" $5}' >> size
        echo "" >> size

        need_add_times=`expr $need_add_times + 1`
    done

    #递增，控制实验次数
    base_num=$1
    need_add_times=0
    cur_experiments_times=`expr $cur_experiments_times + 1`
done

